home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MacRadioButtonUI.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  207 lines

  1. /*
  2.  * @(#)MacRadioButtonUI.java    1.7 98/02/02
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.mac;
  22.  
  23. import com.sun.java.swing.*;
  24. import com.sun.java.swing.border.*;
  25. import com.sun.java.swing.plaf.basic.BasicRadioButtonUI;
  26.  
  27. import java.awt.*;
  28. import java.awt.event.*;
  29. import com.sun.java.swing.plaf.*;
  30. import java.io.Serializable;
  31.  
  32. /**
  33.  * RadioButtonUI implementation for BasicRadioButtonUI
  34.  * <p>
  35.  * Warning: serialized objects of this class will not be compatible with
  36.  * future swing releases.  The current serialization support is appropriate
  37.  * for short term storage or RMI between Swing1.0 applications.  It will
  38.  * not be possible to load serialized Swing1.0 objects with future releases
  39.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  40.  * baseline for the serialized form of Swing objects.
  41.  *
  42.  * @version @(#)MacRadioButtonUI.java    1.0 11/24/97
  43.  * @author Symantec
  44.  */
  45. public class MacRadioButtonUI extends BasicRadioButtonUI
  46. {
  47.     protected ActivationHelper activationHelper;
  48.     protected ChangeListener changeListener;
  49.     protected boolean isActive = true;
  50.  
  51.     public static ComponentUI createUI(JComponent c)
  52.     {
  53.         return new MacRadioButtonUI();
  54.     }
  55.  
  56.     public boolean isActive()
  57.     {
  58.         return isActive;
  59.     }
  60.  
  61.     /************************** The View *************************/
  62.     
  63.  
  64.     /**
  65.      * paint the radio button
  66.      */
  67.     public synchronized void paint(Graphics g, JComponent c)
  68.     {
  69.         AbstractButton b = (AbstractButton) c;
  70.         ButtonModel model = b.getModel();
  71.         
  72.         Dimension size = c.getSize();
  73.         
  74.         int w = size.width;
  75.         int h = size.height;
  76.         
  77.         Font f = c.getFont();
  78.         g.setFont(f);
  79.         FontMetrics fm = g.getFontMetrics();
  80.         
  81.         Rectangle viewRect = new Rectangle(size);
  82.         Rectangle iconRect = new Rectangle();
  83.         Rectangle textRect = new Rectangle();
  84.         Insets i = c.getInsets();
  85.         
  86.         viewRect.x += i.left;
  87.         
  88.         Icon altIcon = b.getIcon();
  89.         Icon selectedIcon = null;
  90.         Icon disabledIcon = null;
  91.         
  92.         String text = SwingUtilities.layoutCompoundLabel(
  93.                             fm, b.getText(), altIcon != null ? altIcon : icon,
  94.                             b.getVerticalAlignment(), b.getHorizontalAlignment(),
  95.                             b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
  96.                             viewRect, iconRect, textRect, getDefaultTextIconGap(b)
  97.                         );
  98.         
  99.         // fill background
  100.         if (c.isOpaque())
  101.         {
  102.             g.setColor(b.getBackground());
  103.             g.fillRect(0,0, size.width, size.height); 
  104.         }
  105.         
  106.         
  107.         // Paint the radio button
  108.         if (altIcon != null)
  109.         { 
  110.             
  111.             if (!model.isEnabled() || !isActive)
  112.             {
  113.                 altIcon = b.getDisabledIcon();
  114.             }
  115.             else if (model.isPressed() && model.isArmed())
  116.             {
  117.                 altIcon = b.getPressedIcon();
  118.                 if (altIcon == null)
  119.                 {
  120.                     // Use selected icon
  121.                     altIcon = b.getSelectedIcon();
  122.                 } 
  123.             }
  124.             else if (model.isSelected())
  125.             {
  126.                 altIcon = b.getSelectedIcon();
  127.             }
  128.             else if (b.isRolloverEnabled() && model.isRollover())
  129.             {
  130.                 altIcon = (Icon) b.getRolloverIcon();
  131.             } 
  132.             
  133.             if (altIcon == null)
  134.             {
  135.                 altIcon = b.getIcon();
  136.             }
  137.             
  138.             altIcon.paintIcon(c, g, iconRect.x, iconRect.y);
  139.  
  140.         }
  141.         else
  142.         {
  143.             icon.paintIcon(c, g, iconRect.x, iconRect.y);
  144.         }
  145.         
  146.         
  147.         // Draw the Text
  148.         if (text != null)
  149.         {
  150.  
  151.             if (model.isEnabled() && isActive)
  152.             {
  153.                 // *** paint the text normally
  154.                 g.setColor(b.getForeground());
  155.             }
  156.             else
  157.             {
  158.                 // *** paint the text disabled
  159.                 g.setColor(UIManager.getColor("Control.disabledForeground"));
  160.             }
  161.             g.drawString(text, textRect.x, textRect.y + fm.getAscent());
  162.  
  163.             if (b.hasFocus() && b.isFocusPainted() && textRect.width > 0 && textRect.height > 0 )
  164.             {
  165.                 paintFocus(g,textRect,size);
  166.             }
  167.         }
  168.     }
  169.  
  170.     protected void installListeners(JComponent c)
  171.     {
  172.         super.installListeners(c);
  173.         
  174.         activationHelper    = new ActivationHelper(c);
  175.         changeListener        = new ChangeListener();
  176.         activationHelper.addPropertyChangeListener(changeListener);
  177.     }
  178.     
  179.     protected void uninstallListeners(JComponent c)
  180.     {
  181.         super.uninstallListeners(c);
  182.         activationHelper.removePropertyChangeListener(changeListener);
  183.         activationHelper = null;
  184.         changeListener = null;
  185.     }
  186.  
  187.     class ChangeListener implements java.beans.PropertyChangeListener
  188.     {
  189.         public void propertyChange(java.beans.PropertyChangeEvent evt)
  190.         {
  191.             if(evt.getPropertyName().equals("activated"))
  192.             {
  193.                 boolean oldActive = isActive;
  194.                 isActive = ((Boolean) evt.getNewValue()).booleanValue();
  195.                 if(isActive != oldActive)
  196.                 {
  197.                     Object obj = evt.getSource();
  198.                     if(obj instanceof ActivationHelper)
  199.                     {
  200.                         ((ActivationHelper)obj).getComponent().repaint();
  201.                     }
  202.                 }
  203.             }
  204.         }
  205.     }
  206. }
  207.